﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CreatePGSQLDataScript
{
    public partial class FunctionRemark : Form
    {
        public FunctionRemark()
        {
            InitializeComponent();
        }
        DataTable dttable;
        DataAccess.SqlProvider db2;
        string dbName = "";
        private void btnLoad_Click(object sender, EventArgs e)
        {
            LoadTable(txtdbname.Text.Trim());
        }


        private void LoadTable(string dbname)
        {
            string sqlText = string.Format(@"select fnname from tbfunctionRemark where dbname='{0}'", dbname);
            dttable = db2.GetDataTable(sqlText);

            lboxtable.ValueMember = "fnname";
            lboxtable.DisplayMember = "fnname";
            lboxtable.DataSource = dttable;
        }

        private void lboxtable_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int index = lboxtable.SelectedIndex;
            DataRowView drv1 = (DataRowView)lboxtable.Items[index];
            string fnname = drv1["fnname"].ToString();
            txtfnName.Text  = fnname;
            string sqlText = string.Format("select fnDesc from tbfunctionRemark   where dbname='{0}' and fnName='{1}' ", dbName, fnname);
            object obj = db2.ExecuteScalar(sqlText);
            if (obj != null)
            {
                rtxtRemark.Text = obj.ToString();
            }
            else
            {
                rtxtRemark.Text = "";
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string dbname = getCurrDbName();
            string fnname = getCurrFnName();
            string fnDesc = rtxtRemark.Text.Trim();
            string sqlText = string.Format(@"
                    if exists(select 1 from tbfunctionremark where dbname='{0}' and fnname='{1}')
                    begin
	                    update tbfunctionremark set fndesc='{2}' where dbname='{0}' and fnname='{1}';
                    end
                    else
                    begin
	                    insert into tbfunctionremark values('{0}','{1}','{2}');
                    end", dbname, fnname, fnDesc);
            db2.ExecuteSQL(sqlText);
        }

        private string getCurrDbName()
        {
            return txtdbname.Text.Trim();
        }
        private string getCurrFnName()
        {
            if (txtfnName.Text.Trim() != "")
            {
                return txtfnName.Text.Trim();
            }

            int index = lboxtable.SelectedIndex;
            DataRowView drv1 = (DataRowView)lboxtable.Items[index];
            string fnName = drv1["fnname"].ToString();
            return fnName;
        }

        private void FunctionRemark_Load(object sender, EventArgs e)
        {
            dbName = "justgogopg11";
            db2 = new DataAccess.SqlProvider(OisLibrary.DB.Connection);
            db2.Open();
        }
    }
}
